package net.trombonium.steroid;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.app.Activity;
//import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
//import android.webkit.*;

public class Steroid extends Activity implements OnClickListener{
	private static final String TAG = "Steroid";
	public static final String S_ID = "survey id";
	private int surveyId;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        surveyId = this.getIntent().getIntExtra(S_ID, -1);
        if(surveyId==-1){
        	//do something generic
        	(new Manager()).setAlarms(this);
            setContentView(R.layout.start);
	        findViewById(R.id.ButTemp).setOnClickListener(this);
	        findViewById(R.id.ButFetch).setOnClickListener(this);
	        findViewById(R.id.ButDelete).setOnClickListener(this);
	        findViewById(R.id.ButQuit).setOnClickListener(this);
        }
        else{
        	String surveyName = (new DBHelper(this)).getSurveyName(surveyId);
            setContentView(R.layout.main);
        	TextView tv = (TextView) findViewById(R.id.TktTakeSurvey);
        	tv.setText("Do you want to take the survey: "+surveyName);
	        findViewById(R.id.ButYes).setOnClickListener(this);
	        findViewById(R.id.ButQuit).setOnClickListener(this);
        }
    }
    
    public void onClick(View v){
    	switch(v.getId()){
    	case R.id.ButYes:
    		startSurvey();
    		break;
    	case R.id.ButTemp:
    		tempCode();
    		break;
    	case R.id.ButDelete:
    		deletion();
    		break;
    	case R.id.ButFetch:
    		fetchSurveys();
    		break;
    	case R.id.ButQuit:
    		this.finish();
    	}
    }
    
    private void deletion(){
    	(new DBHelper(this)).deleteEverything();
    	finish();
    }
    
    private void tempCode(){
    	//WAInterface waInterface = new  WAInterface();
    	//waInterface.postResults(this, 125);
//		Intent surveyorIntent = new Intent(this, Surveyor.class);
//		surveyorIntent.putExtra(S_ID, 219);
//		startActivity(surveyorIntent);
    	//String sql = (new DBHelper(this)).getResultsSQL(125);
		//(new DBHelper(this)).parseCronjobs();
    	//(new WAInterface()).getSurveys(this);************************************
//    	Notify.setSurveyNotification(this, 167);
//    	(new DBHelper(this)).deleteEverything();
    	startActivity(new Intent(this, SurveyPicker.class));
    	finish();
    }
    
    private void fetchSurveys(){
		Intent getSurveyIntent = new Intent(this, WAInterface.class);
		getSurveyIntent.putExtra(WAInterface.TODO, WAInterface.GET_SURVEYS);
		this.startService(getSurveyIntent);
		this.finish();
    }
    
    public void startSurvey(){
		Intent surveyorIntent = new Intent(this, Surveyor.class);
		surveyorIntent.putExtra(S_ID, surveyId);
		startActivity(surveyorIntent);
    	finish();
    }
}
